-- 传送处理
--#include "data\config\NpcMiscCfg\ChuanSongYuanCfg.lua" once


-- 打开传送员界面
function OpenTeleportDlg(sysarg)
	local nCount = table.getn(ChuanSongYuanCfg.mapTitle);
	local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sSendOpenChuanSongDlg)
	if not pack then return; end
	
	DataPack.writeByte(pack, nCount);			-- 版块数量
	for i=1, nCount do
		local dataCfg = ChuanSongYuanCfg.mapdata[i];
		local btnCount = #dataCfg;
		DataPack.writeString(pack, ChuanSongYuanCfg.mapTitle[i]);	-- 标题
		DataPack.writeByte(pack, btnCount);			-- 按钮数量
		for j=1, btnCount do
			DataPack.writeInt(pack, dataCfg[j][1]);		-- 场景id
			DataPack.writeString(pack, dataCfg[j][5]);	-- 按钮名字
			DataPack.writeString(pack, string.format("mapto,%d,%d", i, j));	-- 回调函数名 + 参数
			DataPack.writeWord(pack, dataCfg[j][3]);	-- 等级
			DataPack.writeByte(pack, dataCfg[j][4]);	-- 转生数
		end
	end
	DataPack.flush(pack);
end


function moveToScene(sysarg, nAreaIdx, nBtnIdx)
	--print("moveToScene, nAreaIdx="..nAreaIdx..", nBtnIdx="..nBtnIdx)
	local cfg = ChuanSongYuanCfg;

	--正在押镖
	if Actor.getIsDartFlag(sysarg) == true then
		Actor.sendTipmsg(sysarg, Lang.Activity.g00030, ttFlyTip)
		return false
	end

	if nAreaIdx < 1 or nAreaIdx > 4 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0002, ttFlyTip);
		return false
	end
	
	local data = cfg.mapdata[nAreaIdx][nBtnIdx];
	if not data or #data == 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0002, ttFlyTip);
		return false
	end
	
	local plv = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL);
	local circle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE);
	
	-- 满足等级或转生
	if not Actor.checkActorLevel(sysarg, data[3], data[4]) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.cs00001, data[5], data[6]), ttFlyTip)
		return false
	end
	
	if data[1] <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0002, ttFlyTip);
		return false
	end

	local vipLimit = data[7]
	if vipLimit and vipLimit > 0 and not CheckVip( sysarg, vipLimit ) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.COM0050, vipLimit), ttFlyTip);
		return false
	end
	
	-- 还没完成
	if nAreaIdx == 3 then			-- 安全区
		local npcPtr = Fuben.getNpcPtrById(data[1], data[2]);
		if npcPtr then			--有NPC，传到NPC身边
			local x, y = Actor.getEntityPosition(npcPtr, 0, 0)
			if System.telportScene(sysarg, data[1], x, y - 1) == true then
				Actor.OnNpcTalk(sysarg, npcPtr, "");
			end
		else 					--没有NPC，传到场景
			if Actor.canTelport(sysarg, data[1]) == false then
				Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0002, ttFlyTip);
				return;
			end
		
			local mname = Fuben.getSceneNameById(data[1]);
			System.telportSceneDefaultPoint(sysarg, mname);
		end
	else
		if Actor.canTelport(sysarg, data[1]) == false then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0002, ttFlyTip);
			return
		end
		
		local sceneId = data[1]
		local teleId = data[8] or 0
		local guildId = Actor.getGuildId( sysarg )
		--print("moveToScene, sceneId="..sceneId..", guildId="..guildId..", teleId="..teleId)
		if teleId > 0 and guildId > 0 and IsGuildOwnScene( guildId, sceneId ) then		--占领行会
			--print("moveToScene, teleId="..teleId)
			CommonTeleportByTeleId( sysarg, teleId )
		else
			local mname = Fuben.getSceneNameById(data[1])
			--print("moveToScene, mname="..mname)
			System.telportSceneDefaultPoint(sysarg, mname)
		end
	end
	return true
end


-- 点击传送界面
MiscsImplement.onClickTeleport = function( sysarg, msgid, packet )
	if msgid ~= LogicCmd.miscSys.sub.cClickDlgTeleport then
		return
	end
	
	local nAreaIdx = DataPack.readByte(packet);
	local nBtnIdx = DataPack.readByte(packet);
	
	
	-- 扣除传送石
	local cfg = BackToCity[3];
	local itemPtr = Item.getBagItemPtrById(sysarg, cfg.item_id)
	delCount = Item.removeItemDura(sysarg, itemPtr, 1, Lang.LogTips.l00112, 2, true)
	if delCount < 1 then
		delCount = Actor.removeItemByPtr(sysarg, itemPtr, 1, true, Lang.LogTips.l00112, 2)
		return false
	end
	
	return moveToScene(sysarg, nAreaIdx, nBtnIdx);
end


MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cClickDlgTeleport, MiscsImplement.onClickTeleport)
